// let baseurl = "http://192.168.43.95:8080"
import qs from "qs"
// let baseurl = "http://192.168.50.89:8080"
// let baseurl = "http://192.168.77.97:8080"
let baseurl = "http://8.137.22.10:8080"

 //自动登录 在mine页面调用
function autologin(){
	return new Promise((resolve,reject)=>{
		uni.getStorage({
		key:"token",
		success(res) {
			if(res.data){
				//保存到
				uni.request({
					//将token发送后端，需要返回用户信息 用于展示
					url: baseurl+"/user/userInfo",
					method:"GET",
					header:{
						"Authorization":res.data
					},
					success(r) {
						//成功之后对接收结果进行处理
						if(r.statusCode === 401){
							uni.showToast({
								title:"登录已过期",
								icon:"error"
							})
							setTimeout(()=>{
								uni.navigateTo({
									url:"/pages/router/login/login"
								})
							},1000)
						}else{
							resolve({user:r,token:res.data})
						}
						
					},
					fail(err){
						console.log("自动登录失败",err);
						reject(err)
					}
				})
			}else{
				uni.showToast({
					title:"还未登录",
					icon:"error"
				})
				setTimeout(()=>{
					uni.navigateTo({
						url:"/pages/router/login/login"
					})
				},1000)
			}
		},
		fail() {
			uni.showToast({
				title:"还未登录",
				icon:"error"
			})
			setTimeout(()=>{
				uni.navigateTo({
					url:"/pages/router/login/login"
				})
			},1000)
		}
	})
	})
	
}
//获取文章详情
function getdetail(token,id){
	return new Promise((resolve,reject)=>{
		uni.request({
			url:baseurl + "/article/detail?id="+id,
			method:"GET",
			header:{
				"Authorization":token
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}
//分页查询
function getpage(token,pageNum,pageSize){
	return new Promise((resolve,reject)=>{
		let qurl = ""
		if(pageNum){
			qurl = baseurl + "/article/all?pageNum="+pageNum + "&pageSize="+pageSize + "&status=2"
		}else{
			qurl =  baseurl + "/article/all"
		}
		uni.request({
			url:qurl,
			method:"GET",
			header:{
				"Authorization":token
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

//查询用户的文章
function getuserpage(token,pageNum,pageSize){
		return new Promise((resolve,reject)=>{
			let qurl = ""
			if(pageNum){
				qurl = baseurl + "/article?pageNum="+pageNum + "&pageSize="+pageSize
			}else{
				qurl =  baseurl + "/article/getWithoutPb"
			}
			uni.request({
				url:qurl,
				method:"GET",
				header:{
					"Authorization":token
				},
				success(r) {
					resolve(r)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	}

//根据文章id查用户
function getuserbyid(token,pageid){
	return new Promise((resolve,reject)=>{
		let qurl =  baseurl + "/article/getUserByArticleId?articleId="+pageid
		uni.request({
			url:qurl,
			method:"GET",
			header:{
				"Authorization":token
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}
//根据id查用户
function getuserid(token,id){
	return new Promise((resolve,reject)=>{
		let qurl =  baseurl + "/comment/getUserByUserId?id="+id
		uni.request({
			url:qurl,
			method:"GET",
			header:{
				"Authorization":token
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

//获取评论
function getcomment(token,pageid){
	return new Promise((resolve,reject)=>{
		let qurl =  baseurl + "/comment?id="+Number(pageid)
		uni.request({
			url:qurl,
			method:"GET",
			header:{
				"Authorization":token
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

//添加评论
function addcomment(token,pageid,content){
	return new Promise((resolve,reject)=>{
		let qurl =  baseurl + "/comment"

		uni.request({
			url:qurl,
			method:"POST",
			data:{
				articleId:pageid,
				content:content
			},
			header:{
				"Authorization":token,
				// 'Content-Type': 'application/json',
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}


function getpageType(token){
	return new Promise((resolve,reject)=>{
		let qurl =  baseurl + "/category/all"
		uni.request({
			url:qurl,
			method:"GET",
			header:{
				"Authorization":token,
				// 'Content-Type': 'application/json',
				// 'Content-Type': 'application/x-www-form-urlencoded'
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}
//发布文章
function publicpage(token,data){
	return new Promise((resolve,reject)=>{
		let qurl =  baseurl + "/article"
		uni.request({
			url:qurl,
			method:"POST",
			data:data,
			header:{
				"Authorization":token,
				// 'Content-Type': 'application/json',
				// 'Content-Type': 'application/x-www-form-urlencoded'
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

function search(token,key){
	return new Promise((resolve,reject)=>{
		let qurl =  baseurl + "/article/search"
		uni.request({
			url:qurl,
			method:"POST",
			data:{
				key:key
			},
			header:{
				"Authorization":token,
				// 'Content-Type': 'application/json',
				// 'Content-Type': 'application/x-www-form-urlencoded'
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

//获取审核文章
function getreviewpage(token){
	return new Promise((resolve,reject)=>{
		let qurl =  baseurl + "/article/getReviewArticle"
		uni.request({
			url:qurl,
			method:"GET",
			header:{
				"Authorization":token,
				// 'Content-Type': 'application/json',
				// 'Content-Type': 'application/x-www-form-urlencoded'
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

//删除文章
function delpage(token,data){
	return new Promise((resolve,reject)=>{
		let qurl =  baseurl + "/article/deleteReviewArticle"
		uni.request({
			url:qurl,
			method:"DELETE",
			data:data,
			header:{
				"Authorization":token,
				// 'Content-Type': 'application/json',
				// 'Content-Type': 'application/x-www-form-urlencoded'
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}
//修改编辑文章
function updatepage(token,data){
	return new Promise((resolve,reject)=>{
		let qurl =  baseurl + "/article/updateReviewArticle"
		uni.request({
			url:qurl,
			method:"PUT",
			data:data,
			header:{
				"Authorization":token,
				// 'Content-Type': 'application/json',
				// 'Content-Type': 'application/x-www-form-urlencoded'
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}
//反馈
function feedback(token,data){
	return new Promise((resolve,reject)=>{
		let qurl =  baseurl + "/feedback/add"
		uni.request({
			url:qurl,
			method:"POST",
			data:data,
			header:{
				"Authorization":token,
				// 'Content-Type': 'application/json',
				// 'Content-Type': 'application/x-www-form-urlencoded'
			},
			success(r) {
				resolve(r)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}



module.exports = {
	baseurl,
	autologin,
	getpage, //获取文章
	getuserpage,
	getdetail,//详情
	getuserbyid,   //获取
	getcomment,
	getuserid,
	addcomment,
	getpageType,
	publicpage,
	search,
	getreviewpage,
	delpage,
	updatepage,
	feedback
}

